package com.edroid.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public final class ReflectHelper {

	@SuppressWarnings("unchecked")
	public static <T> T getF(Object src, String name) {
		try {
			Field field = src.getClass().getDeclaredField(name);
			field.setAccessible(true);
			return (T) field.get(src);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static void setF(Object dst, String name, Object value) {
		setF2(dst, dst.getClass(), name, value);;
	}
		
	public static void setF2(Object dst, Class<?> dstClass, String name, Object value) {
		try {
			Field field = dstClass.getDeclaredField(name);
			field.setAccessible(true);
			field.set(dst, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T inv1(Object src, String name, Class<?>[] argTypes, Object[] args) {
		try {
			Method method = src.getClass().getDeclaredMethod(name, argTypes);
			method.setAccessible(true);
			return (T) method.invoke(src, args);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
		
	@SuppressWarnings("unchecked")
	public static <T> T inv0(Object src, String name) {
		try {
			Method method = src.getClass().getDeclaredMethod(name);
			method.setAccessible(true);
			return (T) method.invoke(src);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
